package com.douglas.managesystem.controller.student;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.entity.studentpojo.Instruct;
import com.douglas.managesystem.service.student.ClassService;
import com.douglas.managesystem.service.student.LessonService;
import com.douglas.managesystem.service.student.TeacherService;
import com.douglas.managesystem.service.system.InstructService;
import com.douglas.managesystem.vo.InstructVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description 授课管理
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-21 0:26
 **/
@SuppressWarnings("all")
@Controller
@RequestMapping("/stu")
public class InstructController {


    @Autowired
    private InstructService instructService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ClassService classService;

    @Autowired
    private LessonService lessonService;
    /**
     * 1.分页查询
     * @param model
     * @return
     */
    @GetMapping("/instruct/list")
    public String toList(Model model, SearchEntity params){
        PageInfo<InstructVO> page = instructService.queryListByPage(params);
        model.addAttribute("instruct",page);
        return "admin/instruct/list";
    }

    /**
     * 2.去新增页面
     * @param model
     * @return
     */
    @GetMapping("/instruct/toAdd")
    public String toAdd(Model model){
        List<String> teacherIdList = teacherService.queryAllTeacherList();
        List<String> classIdList = classService.queryAllClassList();
        List<String> lessonIdList =  lessonService.queryAllLessList();
        model.addAttribute("teacherIds",teacherIdList);
        model.addAttribute("classIds",classIdList);
        model.addAttribute("lessonIds",lessonIdList);
        return "admin/instruct/add";
    }



    /**
     * 3.新增授课对象
     * @param instruct
     * @return
     */
    @PostMapping("/instruct/add")
    public String addOrEdit(Instruct instruct){
        if(StringUtils.isEmpty(instruct.getId())){
            System.out.println(instruct);
            instructService.saveInstruct(instruct);
        }else{
            instructService.updateInstruct(instruct);
        }
        return "redirect:/stu/instruct/list";
    }


    /**
     * 4.去更新页面
     * @param model
     * @return
     */
    @GetMapping("/instruct/toEdit")
    public String toEdit(Model model,String id){
        List<String> teacherIdList = teacherService.queryAllTeacherList();
        List<String> classIdList = classService.queryAllClassList();
        List<String> lessonIdList =  lessonService.queryAllLessList();
        model.addAttribute("teacherIds",teacherIdList);
        model.addAttribute("classIds",classIdList);
        model.addAttribute("lessonIds",lessonIdList);
        Instruct instruct = instructService.findByInstructId(id);
        model.addAttribute("instruct",instruct);
        return "admin/instruct/edit";
    }

    /**
     * 5.删除授课信息
     * @param id
     * @return
     */
    @GetMapping("/instruct/delete")
    public String toDelete(String id){
        instructService.deleteById(id);
        return "redirect:/stu/instruct/list";
    }

}
